#include "MGUI_Sprite.h"

namespace Myway {

	ImplementRTTI(MGUI_Sprite, MGUI_Widget);

	MGUI_Sprite::MGUI_Sprite(const MGUI_LookFeel * _lookfeel, MGUI_Widget * _parent, bool _genPickMask)
		: MGUI_Widget(_lookfeel, _parent)
	{
		if (_lookfeel == NULL || !_genPickMask)
			return ;

		const MGUI_Rect & uvRect = _lookfeel->GetUVRect(MGUI_WidgetState::Normal);
		Texture * skin = _lookfeel->GetSkin();

		if (skin == NULL && skin->GetFormat() != PF_A8R8G8B8)
			return ;

		int width = uvRect.x1 - uvRect.x0 + 1;
		int height = uvRect.y1 - uvRect.y0 + 1;
		Array<bool> masks(width * height);

		LockedRect rect;
		skin->Lock(&rect, LK_READ);

		int index = 0;
		unsigned int * src = (unsigned int *)rect.pData + rect.Pitch / 4 * uvRect.y0 + uvRect.x0;
		for (int j = 0; j < height; ++j)
		{
			for (int i = 0; i < width; ++i)
			{
				masks[index++] = (M_RGBA_A(src[i]) > 0);
			}

			src += rect.Pitch / 4;
		}

		skin->Unlock();

		SetPickMask(&masks[0], width, height);

		mPickMaskEnable = true;
	}

	MGUI_Sprite::~MGUI_Sprite()
	{
	}

}